/**
 * Created by 15088 on 2017/7/23.
 */
layui.config({
    base: '/static/js/layuiModulesEX/'
});
layui.use(['paging', 'form', 'layer','formverify'], function () {
    var $ = layui.jquery,
        paging = layui.paging(),
        layer = layui.layer,
        form = layui.form(),
        formverify = layui.formverify;

    var schoolAdminUser = {};

    //分页组件
    paging.init({
        openWait: true,
        url: '/university/findUniversityAndSchoolsByCondition2',
        elem: '#content', //内容容器
        params: { //发送到服务端的参数
        },
        contentType: "application/json; charset=utf-8",
        type: 'POST',
        tempElem: '#tpl', //模块容器
        pageConfig: { //分页参数配置
            elem: '#paged', //分页容器
            pageSize: 3 //分页大小
        },
        success: function () { //渲染成功的回调
            //alert('渲染成功');
        },
        fail: function (msg) { //获取数据失败的回调
            //alert('获取数据失败')
        },
        complate: function () { //完成的回调
            //alert('处理完成');
            $("#example-basic").treetable({
                nodeIdAttr: "nodeId",
                parentIdAttr: "parentId",
                stringCollapse: "收起",
                stringExpand: "展开",
                expandable: true,
            },true);
            //绑定所有编辑按钮事件
            $('#content').children('tr').each(function () {
                //获取每行记录的id值
                var $that = $(this).children('td:last-child');
                var id = $that.children('a[data-opt=more]').data('id');
                // console.log(id);
                $that.children('a[data-opt=more]').on('click', function () {

                });
                $that.children('a[data-opt=delete]').on('click', function() {
                    layer.confirm('确认删除？',function (index) {
                        axios.get('/school/removeById/'+id)
                            .then(function (response) {
                                console.log(response);
                            })
                            .catch(function (response) {
                                console.log(response);
                            });
                        layer.close(index);
                        location.reload();

                    });

                    // var id = $that.children('td:fisrt-child').children('input[type=checkbox]').data('id');
                    // layer.msg(id);
                    // layer.msg(id);
                });

            });
        }
    });

    var vm = new Vue({
        el: '#Vue',
        data: {
            show:true
        },
        methods: {
            add: function () {
                var tab = parent.tab;
                tab.tabAdd({
                    href: '/system/addSchoolAndCollege',
                    icon: 'fa-user',
                    title: '创建院校'
                });
            },
            changePwd:function () {
                layer.confirm("确认重置吗？",function () {
                    // axios.post('/user/modifyPwd',{})
                    //     .then(function (response) {
                    //         console.log(response);
                    //     })
                    //     .catch(function (msg) {
                    //         console.log(msg);
                    //     })
                });
            },
            reload:function () {
                vm.show = true;
            },
            /**
             * 创建大学
             */
            addUniversity: function () {
                $.get('/system/addUniversity', null, function (html) {
                    var addBoxIndex = -1;
                    if (addBoxIndex !== -1) //说明
                        return;
                     addBoxIndex = layer.open({
                        type: 1,
                        title: '创建学校',
                        // offset: ['100px', '20%'],
                        area: ['700px', '200px'],
                        content: html
                        ,success: function (layero, index) {
                            //创建学校监听
                            form.on('submit(createUniversity)', function (data) {
                                console.log(data.field);
                                axios.post("/university/addOne", data.field)
                                    .then(function (response) {
                                        layer.msg("创建成功",{icon: 1,time:1500},function () {
                                            paging.get();
                                            window.location.reload();
                                            layer.close(index);
                                        });
                                        console.log(response.data);
                                    })
                                    .catch(function (response) {
                                        layer.msg("系统异常，创建失败",{icon: 5,time:1500},function () {
                                            layer.close(index);
                                        });
                                        console.log(response);
                                    });
                                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                            });
                            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                        }
                        ,end: function () {
                            addBoxIndex = -1;
                        }
                    })
                })
            },
            /**
             * 创建学院
             */
            addSchool: function () {
                var addBoxIndex = -1;
                if (addBoxIndex !== -1) //说明已打开
                    return;
                $.get('/system/addSchool', null, function (html) {
                     addBoxIndex = layer.open({
                        type: 1,
                        title: '创建学院',
                        scrollbar: false,
                        // offset: ['100px', '20%'],
                        area: ['700px', '300px'],
                        content: html,
                        success: function (layero, index) {
                            //创建学院监听
                            form.on('submit(createCollege)', function (data) {
                                console.log(data.field);
                                axios.post("/school/addOne", data.field)
                                    .then(function (response) {
                                        if (response.data.success)
                                            layer.msg("创建成功",{icon: 1,time:1500},function () {
                                                paging.get();
                                                window.location.reload();
                                                layer.close(index);
                                            });
                                        else
                                            layer.msg(response.data.message,{icon: 5,time:1500},function () {
                                            });
                                    })
                                    .catch(function (response) {
                                        layer.msg("创建失败",{icon: 5,time:1500},function () {
                                            layer.close(index);
                                        });
                                    });
                                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                            });
                            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                        }, //success End
                        end: function () {
                            addBoxIndex = -1;
                        }
                    }) // layer End
                })// get End
            }//addSchool End
        }//methods End
    });


});
